(function(angular) {
	"use strict";
	angular.module("fs.waf").controller("cls.lookup.settlementInitSplitController", settlementInitSplitController);
	settlementInitSplitController.$inject = [ "$scope", "$q", "$uibModalInstance", "dialogsModule", "gridService", "layoutConfig", "validatorConfig", "$filter", "arithService" ];
	function settlementInitSplitController($scope, $q, $uibModalInstance, dialogsModule, gridService, layoutConfig, validatorConfig, $filter, arithService) {
		var ctrl = this;
		ctrl.dialogsModule = dialogsModule;
		ctrl.validatorConfig = validatorConfig;
		ctrl.split = ctrl.split || {};

		ctrl.init = function() {
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, layoutConfig.formConfigs.split.tableConfigs.table, rowSelectionChanged);
			ctrl.innerGridOptions.data = "vm.split.splitSetlDetailsTmp";
			if (ctrl.dialogsModule.params.mObj) {
				ctrl.split = ctrl.dialogsModule.params.mObj;
				ctrl.ccyOptions = {
					"data" : ctrl.split.gccyOptions
				};
			}
			ctrl.split.splitSetlDetailsTmp = angular.copy(ctrl.split.splitSetlDetails);
			// ==== interest or fee can"t change the currency when splited,
			// these splited interest or fee"s pay currency = their parent pay
			// currency
			ctrl.defalutPayCcy = ctrl.split.txnCcy;
			ctrl.layoutConfig = layoutConfig;
			if (ctrl.split.itemType == "I" || ctrl.split.itemType == "F") {
				ctrl.defalutPayCcy = ctrl.split.parentPayCcyValue;
			}
			ctrl.split.pall = ctrl.split.amtAll;
			angular.forEach(ctrl.split.splitSetlDetailsTmp, function(data, index, array) {
				data.ccy = data.payCcy ? data.payCcy : ctrl.defalutPayCcy;
				data.amt = data.itemAmtSettled;
				// pall keep the total amount of the split principal
				ctrl.split.pall = arithService.add(ctrl.split.pall, data.amt);
			});
			ctrl.layoutConfig = updateLayoutConfig(layoutConfig, ctrl.split.itemType);
		};

		ctrl.ok = function() {
			var retObj = new Object();
			retObj.ccy = ctrl.split.txnCcy;
			retObj.amtAll = ctrl.split.amtAll;
			angular.forEach(ctrl.split.splitSetlDetails, function(data, index, array) {
				var setlDetail = getSetlDetailBySetlItemSeq(ctrl.split.splitSetlDetailsTmp, data.setlItemSeq);
				if (setlDetail) {
					data.amt = setlDetail.amt;
					data.ccy = setlDetail.ccy;
				} else {
					data.amt = new Number(0);
				}
			});

			angular.forEach(ctrl.split.splitSetlDetailsTmp, function(data, index, array) {
				var setlDetail = getSetlDetailBySetlItemSeq(ctrl.split.splitSetlDetails, data.setlItemSeq);
				if (!setlDetail) {
					ctrl.split.splitSetlDetails.push(data);
				}
			});

			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback["ok"] || dialogsModule.btnCallback["ok"](retObj) !== false) {
				$uibModalInstance.close();
			}
		};

		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback["close"] || dialogsModule.btnCallback["close"]() !== false) {
				$uibModalInstance.dismiss();
			}
		};

		ctrl.removeSplit = function(row) {
			return $q(function(resoved, rejected) {
				if (row.entity.amt) {
					ctrl.split.amtAll = arithService.add(ctrl.split.amtAll, row.entity.amt);
				}
				resoved();
			});
		};

		function getSetlDetailBySetlItemSeq(setlDetails, setlItemSeq) {
			var result = null;
			for (var i = 0; i < setlDetails.length; i++) {
				if (setlItemSeq && setlDetails[i].setlItemSeq == setlItemSeq) {
					result = setlDetails[i];
					break;
				}
			}
			return result;
		}

		ctrl.changeSplit = function(rowObj) {
			var amtAll = ctrl.split.pall;
			angular.forEach(ctrl.split.splitSetlDetailsTmp, function(data, index, array) {
				if (data.amt) {
					amtAll = arithService.sub(amtAll, data.amt);
				}
			});
			if (arithService.gt(amtAll, 0)) {
				ctrl.split.amtAll = amtAll;
			} else {
				rowObj.amt = new Number(0);
			}
		};
		ctrl.addSplit = function(row) {
			return $q(function(resoved, rejected) {
				row.ccy = ctrl.defalutPayCcy;
				resoved();
			});
		};

		function updateLayoutConfig(layoutConfig, itemType) {
			layoutConfig.formConfigs.splitAmtAll.gridConfigs.amtAll.label = getSplitLabel(itemType);
			if (itemType == "I" || itemType == "F") {
				layoutConfig.formConfigs.split.tableConfigs.table.columnDefs[1].readonly = true;
			}
			return layoutConfig;
		}

		function getSplitLabel(itemType) {
			var translate = $filter("translate"), infoText = translate("label.settlement.splitwindow.info"), pText = translate("label.settlement.hinttext.principal"), 
			iText = translate("label.settlement.hinttext.interest"), fText = translate("label.settlement.hinttext.fee");
			if (itemType == "P") {
				infoText += " " + pText;
			} else if (itemType == "I") {
				infoText += " " + iText;
			} else if (itemType == "F") {
				infoText += " " + fText;
			}
			return infoText;
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

	}
})(angular);
